dojo.ready(function() {
    document.title = 'Carregando cursos... - Criar disciplina - D.A.R. - Vinipsmaker labs';

    service.getCursos()
    .addCallback(function(result) {
        var buffer = '';
        result.forEach(function(element) {
            buffer += '<option value="'
                + element._id
                + '">'
                + decodeURI(element.nome) + ' (' + element._id + ')'
                + '</option>';
        });
        document.getElementById('curso').innerHTML += buffer;

        document.title = 'Carregando professores... - Criar disciplina - D.A.R. - Vinipsmaker labs';
        service.getProfessores()
        .addCallback(function(result) {
            var buffer = '';
            result.forEach(function(element) {
                buffer += '<option value="'
                    + element._id
                    + '">'
                    + decodeURI(element.nome) + ' (' + element._id + ')'
                    + '</option>';
            });
            document.getElementById('professor').innerHTML += buffer;

            document.title = 'Criar disciplina - D.A.R. - Vinipsmaker labs';
        })
        .addErrback(onErrback);
    })
    .addErrback(onErrback);
});

function onSubmit()
{
    document.title = 'Criando disciplina... - D.A.R. - Vinipsmaker labs';
    service.addDisciplina(encodeURI(document.getElementById('nome').value),
                          document.getElementById('fornecidos').value,
                          document.getElementById('necessarios').value,
                          document.getElementById('curso').value,
                          document.getElementById('professor').value,
                          document.getElementById('obrigatoria').value)
    .addCallback(function(result) {
        document.getElementById('main').innerHTML
        += '<p>Disciplina de <a href="/disciplina.html#' + result._id + '">'
        + decodeURI(result.nome)
        + '</a> criada com sucesso</p>';
        document.title = 'Criar disciplina - D.A.R. - Vinipsmaker labs';
    })
    .addErrback(onErrback);
    return false;
}